﻿using LiveStreaming.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace LiveStreaming
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://api.polyv.net/live/v2/channels/";
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("appId", "fct7mofxfo");
            d.Add("channelPasswd", "12345");
            d.Add("name", "直播测试");
            d.Add("timestamp", GetTimeStamp());
            d.Add("userId", "2d9212fdd2");
            d.Add("sign", GetSign(d));
            //string json = Newtonsoft.Json.JsonConvert.SerializeObject(d);
            string rs = PostData(url, d).Result;
            ChannelRsModel m = Newtonsoft.Json.JsonConvert.DeserializeObject<ChannelRsModel>(rs);
            if (m.code==200)
            {
            }
        }

        /// <summary>
        /// 异步请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="dict"></param>
        /// <returns></returns>
        private static Task<string> PostData(string url, Dictionary<string, string> dict)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
            FormUrlEncodedContent content = new FormUrlEncodedContent(dict);
            var respMsg = client.PostAsync(url, content);

            HttpResponseMessage mess = respMsg.Result;
            Task<string> msgBody = mess.Content.ReadAsStringAsync();
            return msgBody;
        }


        /// <summary>
        /// 跟PHP同样的MD5加密方法
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Md5(string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = string.Empty;
            for (int i = 0; i < b.Length; i++)
            {
                ret += b[i].ToString("x").PadLeft(2, '0');
            }
            return ret;
        }

        public static string GetSign(Dictionary<string,string> d)
        {
            string appSecret = "157ebe5514dc461690847a7643e98b65";
            string[] strArr = d.Keys.ToArray();
            StringBuilder sb = new StringBuilder();
            sb.Append(appSecret);
            foreach (var item in strArr)
            {
                sb.Append(item).Append(d[item]);
            }
            sb.Append(appSecret);
            string signSource = sb.ToString();
            string sign = Md5(signSource).ToUpper();
            return sign;
        }


        /// <summary>
        /// 获得13位的时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            System.DateTime time = System.DateTime.Now;
            long ts = ConvertDateTimeToInt(time);
            return ts.ToString();
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        private static long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }

    }
}
